from typing import List
import json


class Solution:
    def findAndReplacePattern(self, words: List[str], pattern: str) -> List[str]:
        def match(word: str, pattern: str) -> bool:
            mp = {}
            for x, y in zip(word, pattern):
                if x not in mp:
                    mp[x] = y
                elif mp[x] != y:  # word 中的同一字母必须映射到 pattern 中的同一字母上
                    return False
            return True
        return [word for word in words if match(word, pattern) and match(pattern, word)]


def stringToList(input):
    return json.loads(input)

def stringToString(input):
    return json.loads(input)


def main():
    import sys
    import io
    def readlines():
        for line in io.TextIOWrapper(sys.stdin.buffer, encoding='utf-8'):
            yield line.strip('\n')

    lines = readlines()
    while True:
        try:
            line = next(lines)
            words = stringToList(line)

            line = next(lines)
            pattern = stringToString(line)

            ret = Solution().findAndReplacePattern(words, pattern)

            out = str(ret)
            print(out)
        except StopIteration:
            break


if __name__ == '__main__':
    main()